using System;
using System.Linq;
using System.Text;
using DevExpress.Xpo;
using DevExpress.ExpressApp;
using System.ComponentModel;
using DevExpress.ExpressApp.DC;
using DevExpress.Data.Filtering;
using DevExpress.Persistent.Base;
using System.Collections.Generic;
using DevExpress.ExpressApp.Model;
using DevExpress.Persistent.BaseImpl;
using DevExpress.Persistent.Validation;
using DomainComponents.Common;

namespace DomainComponents.XCRM
{
    public enum CustomerStatus
    {
        Inactive = 0,
        Active = 1
    }
    public enum PreferredContactMethod
    {
        Any = 0,
        Email = 1,
        Phone = 2,
        Fax = 3,
        Mail = 4
    }

    [DomainComponent]
    [XafDefaultProperty("Name")]
    [XafDisplayName("Customer")]
    public interface ICustomer
    {
        [PersistentDc]
        string Name { get; }

        IDCUser Owner { get; set; }

        [System.ComponentModel.DefaultValue(CustomerStatus.Active)]
        CustomerStatus Status { get; set; }

        PreferredContactMethod PreferredContactMethod { get; set; }

        [VisibleInListView(false)]
        decimal CreditLimit { get; set; }

        [VisibleInListView(false)]
        bool CreditHold { get; set; }

        [Calculated("Invoices[Status == 'Completed'].Sum(Amount)")]
        decimal SaleAmount { get; }

        [Action(PredefinedCategory.View, Caption = "Activate...", AutoCommit = true,
            ConfirmationMessage = "This operation will set the selected object as Active.", TargetObjectsCriteria = "Status != 'Active'",
            SelectionDependencyType = MethodActionSelectionDependencyType.RequireMultipleObjects)]
        void Activate();

        [Action(PredefinedCategory.View, Caption = "Deactivate...", AutoCommit = true,
            ConfirmationMessage = CustomerLogic.DeactivateConfirmationMessage, TargetObjectsCriteria = "Status = 'Active'",
            SelectionDependencyType = MethodActionSelectionDependencyType.RequireMultipleObjects)]
        void Deactivate();

        [BackReferenceProperty("BackRefCustomer")]
        IList<IInvoice> Invoices { get; }
    }

    [DomainLogic(typeof(ICustomer))]
    public class CustomerLogic
    {
        public const string DeactivateConfirmationMessage =
@"This action will set the object as inactive. There may be records in the system that continue to reference these inactive records.

Do you want to proceed?";

        public static void Deactivate(ICustomer customer)
        {
            customer.Status = CustomerStatus.Inactive;
        }
        public static void Activate(ICustomer customer)
        {
            customer.Status = CustomerStatus.Active;
        }
        //public static IList<IInvoice> Get_Invoices(ICustomer customer, IObjectSpace os) {
        //    return new FilteredList<ISaleBase, IInvoice>(customer.Sales);
        //}
        //public static string Get_DisplayName(ICustomer customer) {
        //    return customer.Name;
        //}
    }


    //[DefaultClassOptions]
    ////[ImageName("BO_Contact")]
    ////[DefaultProperty("DisplayMemberNameForLookupEditorsOfThisType")]
    ////[DefaultListViewOptions(MasterDetailMode.ListViewOnly, false, NewItemRowPosition.None)]
    ////[Persistent("DatabaseTableName")]
    //// Specify more UI options using a declarative approach (http://documentation.devexpress.com/#Xaf/CustomDocument2701).
    //public class ICustomer : BaseObject
    //{ // Inherit from a different class to provide a custom primary key, concurrency and deletion behavior, etc. (http://documentation.devexpress.com/#Xaf/CustomDocument3146).
    //    public ICustomer(Session session)
    //        : base(session)
    //    {
    //    }
    //    public override void AfterConstruction()
    //    {
    //        base.AfterConstruction();
    //        // Place your initialization code here (http://documentation.devexpress.com/#Xaf/CustomDocument2834).
    //    }
    //    //private string _PersistentProperty;
    //    //[XafDisplayName("My display name"), ToolTip("My hint message")]
    //    //[ModelDefault("EditMask", "(000)-00"), Index(0), VisibleInListView(false)]
    //    //[Persistent("DatabaseColumnName"), RuleRequiredField(DefaultContexts.Save)]
    //    //public string PersistentProperty {
    //    //    get { return _PersistentProperty; }
    //    //    set { SetPropertyValue("PersistentProperty", ref _PersistentProperty, value); }
    //    //}

    //    //[Action(Caption = "My UI Action", ConfirmationMessage = "Are you sure?", ImageName = "Attention", AutoCommit = true)]
    //    //public void ActionMethod() {
    //    //    // Trigger a custom business logic for the current record in the UI (http://documentation.devexpress.com/#Xaf/CustomDocument2619).
    //    //    this.PersistentProperty = "Paid";
    //    //}
    //}
}
